package gov.kqqzyyy.monkey.pharmacyscreen.code.ui;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import gov.kqqzyyy.monkey.pharmacyscreen.R;

public class SplashActivity extends AppCompatActivity {

    private static final int SHOW_TIME = 800; //过渡界面展示时间
    private Thread startThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
    }

    //    @SuppressLint("StaticFieldLeak")
    @Override
    protected void onResume() {
        super.onResume();
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {//检查权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        } else {
            openWindow();
        }

    }

    private void openWindow() {
        SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        final String pharmacy_num = mSharedPreferences.getString("pharmacy_preference", "");
        final String window_num = mSharedPreferences.getString("window_preference", "");
        final String window_detail_num = mSharedPreferences.getString("window_detail_preference", "");

        if (startThread == null) {
            startThread = new Thread(() -> {
                SystemClock.sleep(SHOW_TIME);
                Intent intent = null;
                switch (pharmacy_num) {
                    case "1"://西药房
                        if (window_num.equals("0.0")) {
                            intent = new Intent(getApplicationContext(), WMJHScreenActivity.class);
                        } else {
                            intent = new Intent(getApplicationContext(), WMScreenActivity2.class);
                        }
                        break;
                    case "2"://中药房
                        if (window_num.equals("2.0") && window_detail_num.equals("2.2")) {
//                            intent = new Intent(getApplicationContext(), TCMPYScreenActivity.class);
                            intent = new Intent(getApplicationContext(), TCMPYScreenActivity_v2.class);
                        } else {
                            intent = new Intent(getApplicationContext(), TCMDHScreenActivity.class);
                        }
                        break;
                    case "3"://病区药房
                        Toast.makeText(getApplicationContext(), "病区药房暂不启用叫号屏！", Toast.LENGTH_SHORT).show();
                        break;
                    case "4"://沿街药房
                        Toast.makeText(getApplicationContext(), "沿街药房暂不启用叫号屏！", Toast.LENGTH_SHORT).show();
                        break;
                    case "6"://待煎药房
                        intent = new Intent(getApplicationContext(), TCMDHScreenActivity.class);
                        break;
                    case "":
                        intent = new Intent(getApplicationContext(), FragmentPreferences.class);
                        break;
                }
                if (intent != null) {
                    startActivity(intent);
                    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                }
            });
            startThread.start();
        }
    }


    @Override
    protected void onStop() {
        super.onStop();
        stopThread();
        finish();
    }

    private void stopThread() {
        if (startThread != null && startThread.isAlive()) {
            startThread.interrupt();
            startThread = null;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            if (permissions[0].equals(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                    if (serviceIntent != null) {
//                        startService(serviceIntent);
//                    }
                    openWindow();
                } else {//提示没有权限，安装不了咯
                    Toast.makeText(this, "没有存储读写权限，无法启动应用！", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

}
